
cd "/Users/emanuelcoman/Dropbox/My Mac (Emanuel’s MacBook Pro)/Documents/italy_LEGA/eu_pol_replication/"

					*******Figure 1****
					
					*Bring in the V-dem dataset

use "V-Dem-CD-v11.1.dta", clear

keep country_name year v2xel_locelec

keep if country=="Italy"

drop if v2x==.

drop if year<1950

	
			*gen average by decade since 1950
		gen decade=.
		replace decade=1950 if year>1950&year<=1960
		replace decade=1960 if year>1960&year<=1970
		replace decade=1970 if year>1970&year<=1980
		replace decade=1980 if year>1980&year<=1990
		replace decade=1990 if year>1990&year<=2000
		replace decade=2000 if year>2000&year<=2010
		replace decade=2010 if year>2010&year<=2020
		
		by decade, sort: egen vdem_decade=mean(v2xel_locelec)

		twoway  (connected vdem_decade decade, mcolor(black) msize(tiny) lcolor(gray) lpattern(thin) lwidth(thin)), ///				
				 graphregion(color(white)) ylab(,nogrid)  legend(size(small)) ytitle("Local government power") xtitle("") xlabel(#10,valuelabel  labsize(small) angle(45)) ///
				 		xlabel(1950 "1950s" 1960 "1960s" 1970 "1970s" 1980 "1980s" 1990 "1990s" 2000 "2000s" 2010 "2010s")

	
		graph export "figure1.pdf", replace
		

						***Figure 2***
					*Bring in a dataset with support for the EU
					
	import excel "eurobarometer_italy.xlsx", sheet("Sheet1") firstrow clear

gen net_support=pro_EU-anti_EU

gen year=year(date)


gen number=_n

label define number2 1 "Oct-03" 2 "Oct-04" 3 "Apr-05" 4 "Oct-05" 5 "Apr-06" 6 "Apr-07" 7 "Oct-07" 8 "Oct-08" 9 "Apr-09" ///
					10 "Oct-09" 11 "Apr-10" 12 "Apr-11" 13 "Oct-11" 14 "Apr-12" 15 "Oct-12" 16 "Apr-13" 17 "Oct-13" ///
					18 "Apr-14" 19 "Oct-14" 20 "Apr-15" 21 "Oct-15" 22 "Apr-16" 23 "Oct-16" 24 "Apr-17" 25 "Oct-17" 26 "Oct-18"
label values number number2

twoway connected net_support number, ///
		graphregion(color(white)) ylab(,nogrid) ///
xlabel(1(1)26, valuelabel labsize(tiny) angle(45)) xscale(range(1 26)) ///
title("Net Trust in the EU") xtitle("") ytitle("")
		
 graph export "Figure2.pdf", replace
 
						***Figure 3***
						


***First run the main analysis so we only keep the observations included in the sample

use "data_to_run_main.dta", clear

rdrobust eu1 right_adv, vce(cluster locality2)

keep if e(sample)

sort Q005 com
drop if Q005==Q005[_n-1]&com==com[_n-1]

			graph hbar (count), over(year, label(labsize(small))) blabel(bar, format(%2.0f)) ///
			  legend(off) graphregion(color(white)) yscale(r(0 65)) ylab(,nogrid nolabel) ytitle("") title("Local Elections by Year")
			  
	graph export "Figure3.pdf", replace				
	
	
							***Table 1***
							

	use "locality_balance.dta", clear

rdrobust right_maj right_adv

						
*Method	Coef.	Std. 	Err.		z	P>z		[95% Conf.	Interval]
						
*Conventional	-.2499	.22571	-1.1072	0.268	-.692283	.192476
*Robust			-		-		-1.0530	0.292	-.820492	.246979
						

rdrobust right_gov right_adv

						
*Method			Coef.	Std. Err.	z		P>z		[95% Conf.	Interval]
						
*Conventional	.08356	.20314		0.4114	0.681	-.314582	.481704
*Robust			-		-			0.5515	0.581	-.341934	.609723
						
						***Table 2***
	
use "data_to_run_main.dta", clear

		*gender
rdrobust Q002 right_adv, vce(cluster locality2) 
    *  Conventional | -.00048     .08038   -0.0059  0.995   -.158019      .157067
    *        Robust |     -          -     0.0658   0.948   -.193671       .20713

				
		*age
	rdrobust age_categories right_adv, vce(cluster locality2) 
*      Conventional | -.40381     .27478   -1.4696  0.142   -.942356      .134746
*            Robust |     -          -     -1.5632  0.118   -1.09712      .123549
*--------------------------------------------------------------------------------

	*social class ordinal
	recode P181 (4=.) (5=.) /*neither class; DK*/
	rdrobust P181 right_adv, vce(cluster locality2) 
*Conventional	.05058	.16173	0.3127	0.754	-.266409	.367567
*Robust			-		-		0.5135	0.608	-.265417	.453877
	
	*Occupation
	*gen 6 dummy variables as follows
		*1 student
		*2 unemployed
		*3 employed public sector
		*4 employed private sector
		*5 self-employed
		*6 retired
		*7 housekeeper
	
	gen student=.
	replace student=1 if Q180==3
	replace student=0 if student==.&Q180!=12&Q180!=.
	
	rdrobust student right_adv, vce(cluster locality2) 
*Conventional	.08151	.07948	1.0255	0.305	-.074268	.237279
*Robust			-		-		0.9374	0.349	-.09564		.270982

	
	gen unemployed=.
	replace unemployed=1 if Q180==5
	replace unemployed=0 if unemployed==.&Q180!=12&Q180!=.
		rdrobust unemployed right_adv, vce(cluster locality2) 
*Conventional	.08751	.11689	0.7486	0.454	-.141604	.316615
*Robust			-		-		0.7138	0.475	-.18257		.391698


	
	gen private_sector=.
	replace private_sector=1 if Q179==2
	replace private_sector=0 if private_sector==.&Q180!=12&Q180!=.

		rdrobust private_sector right_adv, vce(cluster locality2)
*Conventional	.12475	.09211	1.3545	0.176	-.05577		.305279
*Robust			-		-		1.2876	0.198	-.072019	.347838


	
	gen public_sector=.
	replace public_sector=1 if Q179==1
	replace public_sector=0 if public_sector==.&Q180!=12&Q180!=.

		rdrobust public_sector right_adv, vce(cluster locality2) 
*Conventional	-.03168	.10118	-0.3131	0.754	-.229982	.166626
*Robust			-		-		-0.5802	0.562	-.318339	.172924


	
	gen self_employed=.
	replace self_employed=1 if Q178==5
	replace self_employed=0 if self_employed==.&Q180!=12&Q180!=.
		rdrobust self_employed right_adv, vce(cluster locality2) 
*Conventional	.07266	.08025	0.9053	0.365	-.084636	.22995
*Robust			-		-		0.8669	0.386	-.107135	.277071


	
	gen retired=.
	replace retired=1 if Q180==1
	replace retired=0 if retired==.&Q180!=12&Q180!=.
		rdrobust retired right_adv, vce(cluster locality2)
*Conventional	-.098	.14317	-0.6844	0.494	-.378612	.182621
*Robust			-		-		-0.7391	0.460	-.466843	.211161


	
	gen housekeeper=.
	replace housekeeper=1 if Q180==2
	replace housekeeper=0 if housekeeper==.&Q180!=12&Q180!=.
		rdrobust housekeeper right_adv, vce(cluster locality2) 
*Conventional	.13423	.1653	0.8121	0.417	-.189747	.458213
*Robust			-		-		1.0626	0.288	-.173014	.582788
	
	
	*vote for Camera in 2008 
	gen vote2008_right=.
	replace vote2008_right=0 if vote2008!=.
	replace vote2008_right=1 if vote2008==4|vote2008==5
	rdrobust vote2008_right right_adv, vce(cluster locality2) /*vote for either ppl or lega*/
*Conventional	.10456	.09276	1.1272	0.260	-.077244	.286372
*Robust			-		-		1.3490	0.177	-.066315	.359153

	

				***Figure 3***
				
use "data_to_run_main.dta", clear

sort reg com
drop if reg==reg[_n-1]&com==com[_n-1]

				
rddensity right_adv

capture preserve
*

capture drop temp_*
qui rddensity right_adv, plot plot_range(-30 30) hist_range(-30 30) genvars(temp)

local ci_plot_region_l = `"(rarea temp_cil temp_cir temp_grid if temp_group == 0, sort lcolor(white%0) color(red%30))"'
local ci_plot_region_r = `"(rarea temp_cil temp_cir temp_grid if temp_group == 1, sort lcolor(white%0) color(blue%30))"'

local es_plot_line_l = `"(line temp_f temp_grid if temp_group == 0, sort lcolor(red) lwidth("medthin") lpattern(solid))"'
local es_plot_line_r = `"(line temp_f temp_grid if temp_group == 1, sort lcolor(blue) lwidth("medthin") lpattern(solid))"'

qui su temp_hist_width if temp_hist_group == 0
local hist_width_l = r(mean)
qui su temp_hist_width if temp_hist_group == 1
local hist_width_r = r(mean)

local plot_histogram_l = `"(bar temp_hist_height temp_hist_center if temp_hist_group == 0, barwidth(`hist_width_l') color(red%20))"'
local plot_histogram_r = `"(bar temp_hist_height temp_hist_center if temp_hist_group == 1, barwidth(`hist_width_r') color(blue%20))"'

local graph_opt = `"xline(0, lcolor(black) lwidth(medthin) lpattern(solid)) graphregion(color(white)) ylab(,nogrid) legend(off) title("`i'", color(gs0)) xtitle("Score variable") ytitle("")"'

twoway 	`plot_histogram_l' 	/// 
				`plot_histogram_r' 	/// 
				`ci_plot_region_l' 	///
				`ci_plot_line_l'   	///
				`ci_plot_ebar_l'   	///
				`ci_plot_region_r' 	///
				`ci_plot_line_r'   	///
				`ci_plot_ebar_r'   	///
				`es_plot_line_l' 	///
				`es_plot_point_l' 	///
				`es_plot_line_r' 	///
				`es_plot_point_r' 	///
				,					///
				`graph_opt'

graph export "Figure3.pdf", replace




					***Figure 4***
					
local DV eu2 eu3

*run the models
foreach j of local DV{
	use "data_to_run_main.dta", clear

		cls
rdrobust `j' right_adv, vce(cluster locality2)
rdrobust `j' right_adv if year<2012, vce(cluster locality2)

		translate @Results "main_results`j'.txt", replace

}	
		
*clean the results
	local DV eu2 eu3

foreach j of local DV{
import delimited "main_results`j'.txt", clear

**keep confidence intervals first
keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)




split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2|_n==4
replace stat="N" if _n==1|_n==3

gen low=results6[2] if _n<3
gen up=results7[2]  if _n<3

replace low=results6[4] if _n>2&_n<5
replace up=results7[4] if _n>2&_n<5



gen NL=results2[1] if _n<3
gen NR=results3[1] if _n<3
replace NL=results2[3] if _n>2&_n<5
replace NR=results3[3] if _n>2&_n<5


gen coef2=results2[2] if _n<3
replace coef2=results2[4] if _n>2&_n<5

keep if stat=="coeff"


gen low_regular95=real(low)
gen up_regular95=real(up) 
gen eff_N_left=real(NL)
gen eff_N_right=real(NR)
gen coeff=real(coef2)


keep low_-coeff


gen sample="all years" if _n==1
replace sample="<2012" if _n==2

gen DV="`j'"


save "results`j'.dta", replace
}


****merge the results in one document
use "resultseu2.dta", clear
save "FINAL_main_results.dta", replace

		
local DV eu3

foreach j of local DV {
use "FINAL_main_results.dta", clear
merge m:m DV sample using "results`j'.dta"
drop _merge
save "FINAL_main_results.dta", replace

}


******Graph the results in figure 4
					
	use "FINAL_main_results.dta", clear	
	
*declare dataset as matrix

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

replace sample="before_2012" if sample=="<2012"

		sort DV sample
		
mkmat coef low_regular95 up_regular95 if sample=="all years"&DV=="eu2", mat(all_years)
  mkmat coef low_regular95 up_regular95 if sample=="before_2012"&DV=="eu2", mat(before_2012)

 coefplot (matrix(all_years[,1]), ci((all_years[,2] all_years[,3]))) ///
		(matrix(before_2012[,1]), ci((before_2012[,2] before_2012[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
		xscale(range(-.3 .6)) xlabel (-.2(.2).6) ///
		yscale(off) ///
		legend (order(2 "All years" 4 "Before 2012")) ///
		fxsize(100) title("Important problem") saving(fig1_eu2, replace)
		
mkmat coef low_regular95 up_regular95 if sample=="all years"&DV=="eu3", mat(all_years)
  mkmat coef low_regular95 up_regular95 if sample=="before_2012"&DV=="eu3", mat(before_2012)

keep if DV=="eu3"
  
 coefplot (matrix(all_years[,1]), ci((all_years[,2] all_years[,3]))) ///
		(matrix(before_2012[,1]), ci((before_2012[,2] before_2012[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
			xscale(range(-.3 .6)) ///
				yscale(off) ///
		legend (order(2 "All years" 4 "Before 2012")) ///
		fxsize(100) title("Important problem-ordinal") saving(fig1_eu3, replace)
		
		 grc1leg fig1_eu2.gph fig1_eu3.gph, cols(3) graphregion(color(white)) saving(fig1_FINAL, replace)

 graph export "Figure4.pdf", replace
 
 
 
						***Figure 5***

*run the models

local DV eu2 eu3
	
foreach j of local DV{
	use "data_to_run_main.dta", clear

		cls
rdrobust `j' right_adv if year<2012&right_2008==0, vce(cluster locality2)
rdrobust `j' right_adv if year<2012&right_2008==1, vce(cluster locality2)

		translate @Results "hetero_results`j'.txt", replace

}	
	
*clean the results

	local DV eu2 eu3

foreach j of local DV{
import delimited "hetero_results`j'.txt", clear

**keep confidence intervals first
keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)




split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2|_n==4
replace stat="N" if _n==1|_n==3

gen low=results6[2] if _n<3
gen up=results7[2]  if _n<3

replace low=results6[4] if _n>2
replace up=results7[4] if _n>2



gen NL=results2[1] if _n<3
gen NR=results3[1] if _n<3
replace NL=results2[3] if _n>2
replace NR=results3[3] if _n>2


gen coef2=results2[2] if _n<3
replace coef2=results2[4] if _n>2

keep if stat=="coeff"


gen low_regular95=real(low)
gen up_regular95=real(up) 
gen eff_N_left=real(NL)
gen eff_N_right=real(NR)
gen coeff=real(coef2)


keep low_-coeff


gen sample="right_2008" if _n==1
replace sample="notright_2008" if _n==2

gen DV="`j'"


save "hetero_results`j'.dta", replace
}


****merge the results
use "hetero_resultseu2.dta", clear
save "FINAL_hetero_results.dta", replace

		
local DV eu3

foreach j of local DV {
use "FINAL_hetero_results.dta", clear
merge m:m DV sample using "hetero_results`j'.dta"
drop _merge
save "FINAL_hetero_results.dta", replace

}

******Graph the results 
					
	use "FINAL_hetero_results.dta", clear	
		****merge these results with results from continuity based analysis

	
*declare dataset as matrix

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

		
		
mkmat coef low_regular95 up_regular95 if sample=="notright_2008"&DV=="eu2", mat(non_attached_voters)
  mkmat coef low_regular95 up_regular95 if sample=="right_2008"&DV=="eu2", mat(attached_voters)

 coefplot (matrix(non_attached_voters[,1]), ci((non_attached_voters[,2] non_attached_voters[,3]))) ///
		(matrix(attached_voters[,1]), ci((attached_voters[,2] attached_voters[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
		xscale(range(-.12 .12)) xlabel(-1(.5)1) ///
						yscale(off) ///
		legend (order(2 "Non-attached voters" 4 "Attached voters")) ///
		fxsize(100) title("Important problem") saving(fig2_het_eu2, replace)
		
mkmat coef low_regular95 up_regular95 if sample=="notright_2008"&DV=="eu3", mat(non_attached_voters)
  mkmat coef low_regular95 up_regular95 if sample=="right_2008"&DV=="eu3", mat(attached_voters)

  keep if DV=="eu3"
  
 coefplot (matrix(non_attached_voters[,1]), ci((non_attached_voters[,2] non_attached_voters[,3]))) ///
		(matrix(attached_voters[,1]), ci((attached_voters[,2] attached_voters[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
		xscale(range(-.12 .12)) ///
						yscale(off) ///
		legend (order(2 "Non-attached voters" 4 "Attached voters")) ///
		fxsize(100) title("Important problem-Ordinal") saving(fig2_het_eu3, replace)
		
		 grc1leg fig2_het_eu2.gph fig2_het_eu3.gph, cols(3) graphregion(color(white)) saving(fig2_FINAL, replace)

 graph export "FINAL_figure5.pdf", replace
 
 
 
						***Appendix***
						
			
						***Table A1***
						
		********Local randomization*****		
		
		****First do eu2
	
			*Panel I, All years
		local band 2 3 4
foreach i of local band{		
		use "data_to_run_main.dta", clear
		
			
rdrandinf eu2 right_adv, level(.05) interfci(.05) wl(-`i') wr(`i')
		
}
			*Panel I, before 2012
	local band 2 3 4
foreach i of local band{		
		use "data_to_run_main.dta", clear
		
rdrandinf eu2 right_adv if year<2012, level(.05) interfci(.05) wl(-`i') wr(`i')
		
}

			*Panel II, All years

	local band 2 3 4
foreach i of local band{		
		use "data_to_run_main.dta", clear
		

			
rdrandinf eu3 right_adv, level(.05) interfci(.05) wl(-`i') wr(`i')
		
}	

			*Panel II, before 2012

	local band 2 3 4
foreach i of local band{		
		use "data_to_run_main.dta", clear

rdrandinf eu3 right_adv if year<2012, level(.05) interfci(.05) wl(-`i') wr(`i')
		
}		

	
					***Figure A1***
					

local band 3 4 5 6 7 8 9 
local DV eu2 eu3

foreach j of local DV {
foreach x of local band{
 			use "data_to_run_main.dta", clear

		cls
		rdrobust `j' right_adv if year<2012, h(`x' `x') vce(cluster locality2)
 		rdrobust `j' right_adv if year<2012, h(`x'+.5 `x'+.5) vce(cluster locality2)


translate @Results "diff_band_`j'_`x'.txt", replace
}
}

			
*******Bring in and clean the results


local band  3 4 5 6 7 8 9
local DV eu2 eu3

foreach j of local DV {
foreach x of local band{

import delimited "diff_band_`j'_`x'.txt", clear

keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)



split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}


capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2|_n==4
replace stat="N" if _n==1|_n==3



gen low_regular95=real(results6)
gen up_regular95=real(results7) 
gen eff_N_left=real(results2)
gen coeff=eff_N_left if _n==2|_n==4
replace eff_N_left=. if _n==2|_n==4

gen eff_N_right=real(results3)
replace eff_N_right=. if _n==2|_n==4

replace eff_N_right=eff_N_right[_n-1] if eff_N_r==.
replace eff_N_left=eff_N_left[_n-1] if eff_N_l==.

gen nr=1 if _n==1|_n==2
replace nr=2 if _n==3|_n==4


gsort nr low_reg
by nr, sort: replace low_reg=low_reg[_n-1] if low_reg==.
by nr, sort: replace up_reg=up_reg[_n-1] if up_reg==.
by nr, sort: replace coef=coef[_n-1] if coef==.

drop if nr==nr[_n-1]


keep low_-eff_N_right

gen threshold="`x'" if _n==1
replace threshold="`x'.5" if _n==2

gen DV="`j'"


save "diff_band_`j'_`x'.dta", replace

}
}


****merge the results
use "diff_band_eu2_3.dta", clear
save "diff_band_combined.dta", replace

		
local band 3 4 5 6 7 8 9

			 
local DV eu2 eu3

foreach j of local DV {
foreach x of local band{
use "diff_band_combined.dta", clear
merge m:m DV threshold using "diff_band_`j'_`x'.dta"
drop _merge
save "diff_band_combined.dta", replace

}
}

*Graph the results
	use "diff_band_combined.dta", clear
	

		gen band=real(threshold)
sort DV band 

drop if threshold=="4"

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

mkmat coef low_regular95 up_regular95 if DV=="eu2", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
		rename(r1 ="3" r2="3.5" r3="4" r4="4.5" r5="5" r6="5.5" r7="6" r8="6.5" r9="7" r10="7.5" r11="8" r12="8.5" r13="9")					xline(0, lcolor(red) lwidth(thin)) ///
					ytitle("Bandwidth") ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem") saving(diff_band_eu2, replace)						
						
mkmat coef low_regular95 up_regular95 if DV=="eu3", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
		rename(r1 ="3" r2="3.5" r3="4" r4="4.5" r5="5" r6="5.5" r7="6" r8="6.5" r9="7" r10="7.5" r11="8" r12="8.5" r13="9") ///
					xline(0, lcolor(red) lwidth(thin))  ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem-Ordinal") saving(diff_band_eu3, replace)
						
					 grc1leg diff_band_eu2.gph diff_band_eu3.gph, cols(2) graphregion(color(white)) saving(figureA1, replace)
				 graph export "FigureA1.pdf", replace	


	
						***Figure A2***
								
		
local threshold 1 2 3 4 5
local DV eu2 eu3

foreach j of local DV {
foreach x of local threshold{
 			use "data_to_run_main.dta", clear
			
		cls
		rdrobust `j' right_adv if year<2012&right_adv<0, c(-`x') h(`x' `x') b(`x' `x') vce(cluster locality2)
 		rdrobust `j' right_adv if year<2012&right_adv>0, c(`x') h(`x' `x') b(`x' `x') vce(cluster locality2)


translate @Results "placebo_tests_`j'_`x'.txt", replace
}
}

			
*******Bring in and clean the results


local threshold 1 2 3 4 5
local DV eu2 eu3

foreach j of local DV {
foreach x of local threshold{

import delimited "placebo_tests_`j'_`x'.txt", clear

keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)



split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}


capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2|_n==4
replace stat="N" if _n==1|_n==3



gen low_regular95=real(results6)
gen up_regular95=real(results7) 
gen eff_N_left=real(results2)
gen coeff=eff_N_left if _n==2|_n==4
replace eff_N_left=. if _n==2|_n==4

gen eff_N_right=real(results3)
replace eff_N_right=. if _n==2|_n==4

replace eff_N_right=eff_N_right[_n-1] if eff_N_r==.
replace eff_N_left=eff_N_left[_n-1] if eff_N_l==.

gen nr=1 if _n==1|_n==2
replace nr=2 if _n==3|_n==4


gsort nr low_reg
by nr, sort: replace low_reg=low_reg[_n-1] if low_reg==.
by nr, sort: replace up_reg=up_reg[_n-1] if up_reg==.
by nr, sort: replace coef=coef[_n-1] if coef==.

drop if nr==nr[_n-1]


keep low_-eff_N_right

gen threshold="-`x'" if _n==1
replace threshold="`x'" if _n==2

gen DV="`j'"


save "placebo_tests_`j'_`x'.dta", replace

}
}


****merge them
use "placebo_tests_eu2_1.dta", clear
save "placebo_tests_combined.dta", replace

		
local threshold 1 2 3 4 5

			 
local DV eu2 eu3

foreach j of local DV {
foreach x of local threshold{
use "placebo_tests_combined.dta", clear
merge m:m DV threshold using "placebo_tests_`j'_`x'.dta"
drop _merge
save "placebo_tests_combined.dta", replace

}
}
		
	*****Graph the results with continuity placebos	
	
	use "placebo_tests_combined.dta", clear
	
	**threshold -1 doesn't work "Estimates might be unreliable due to low number of effective observations."
	
	replace coeff=. if threshold=="-1"

	gen threshold2=real(threshold)
sort DV threshold2 

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

mkmat coef low_regular95 up_regular95 if DV=="eu2", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
		rename(r1 ="-5" r2="-4" r3="-3" r4="-2" r5="-1" r6="1" r7="2" r8="3" r9="4" r10="5") ///
					xline(0, lcolor(red) lwidth(thin)) ///
					ytitle("Threshold") ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem") saving(eu2, replace)						
						
mkmat coef low_regular95 up_regular95 if DV=="eu3", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
		rename(r1 ="-5" r2="-4" r3="-3" r4="-2" r5="-1" r6="1" r7="2" r8="3" r9="4" r10="5") ///
					xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem-Ordinal") saving(eu3, replace)
						
					 grc1leg eu2.gph eu3.gph, cols(2) graphregion(color(white)) saving(placebos, replace)
				 graph export "FigureA2.pdf", replace	
	
	
						***Figure A3***
	
local hole 1 2 3 4 5
local DV eu2 eu3

foreach j of local DV {
foreach x of local hole{
 			use "data_to_run_main.dta", clear

			
		cls
rdrobust `j' right_adv if year<2011&right_adv<-`x'*.2|year<2011&right_adv>`x'*.2, vce (cluster locality2)

				
translate @Results "donut`j'_`x'.txt", replace
}
}

*******Bring in and clean the results


local hole 1 2 3 4 5
local DV eu2 eu3

foreach j of local DV {
foreach x of local hole{

import delimited "donut`j'_`x'.txt", clear


keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)




split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}


capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2
replace stat="N" if _n==1

gen low=results6[2]
gen up=results7[2]
gen NL=results2[1]
gen NR=results3[1]
gen coef2=results2[2]



gen low_regular95=real(low)
gen up_regular95=real(up) 
gen eff_N_left=real(NL)
gen eff_N_right=real(NR)
gen coeff=real(coef2)


keep low_-coeff

keep in 1

gen hole="`x'"
gen DV="`j'"


save "donut`j'_`x'_cont.dta", replace

}
}

	
****merge the results
use "donuteu2_1_cont.dta", clear
save "donut_figure.dta", replace

		
local hole 1 2 3 4 5

			 
local DV eu2 eu3

foreach j of local DV {
foreach x of local hole{
use "donut_figure.dta", clear
merge m:m DV hole using "donut`j'_`x'_cont.dta"
drop _merge
save "donut_figure.dta", replace

}
}

*Graph results with donut hole ****

	use "donut_figure.dta", clear

sort DV hole 

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

mkmat coef low_regular95 up_regular95 if DV=="eu2", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
					xline(0, lcolor(red) lwidth(thin)) ///
				rename(r1 ="0.2" r2="0.4" r3="0.6" r4="0.8" r5="1") ///
					ytitle ("Radius") ///
				xscale(range(-.1 .35)) xlabel(-.1(.1).3) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		legend(off) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem") saving(donut_eu2, replace)						
						
						

						
mkmat coef low_regular95 up_regular95 if DV=="eu3", mat(Continuity)


 coefplot (matrix(Continuity[,1]), ci((Continuity[,2] Continuity[,3]))), ///
				rename(r1 ="0.2" r2="0.4" r3="0.6" r4="0.8" r5="1") ///
					xline(0, lcolor(red) lwidth(thin))  ///
				xscale(range(-.1 .6)) xlabel(-.1(.1).6) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) mcolor(black) msymbol(S) ///
		legend (off) ///
		graphregion(color(white)) ylab(,nogrid) title("Important Problem-Ordinal") saving(donut_eu3, replace)
						
					 grc1leg donut_eu2.gph donut_eu3.gph, cols(2)  graphregion(color(white)) saving(donut, replace)
				 graph export "FigureA3.pdf", replace	
	

	          ***Figure A4***

 	local DV eu2 eu3


foreach j of local DV{
	use "data_to_run_main.dta", clear

		cls
rdrobust `j' ppl_adv if lega_adv==., vce(cluster locality2)
rdrobust `j' ppl_adv if year<2011&lega_adv==., vce(cluster locality2)
rdrobust `j' ppl_adv if year>=2011&lega_adv==., vce(cluster locality2)

		translate @Results "ppl_results`j'.txt", replace

}	
		
	local DV eu2 eu3

foreach j of local DV{
import delimited "ppl_results`j'.txt", clear

**keep confidence intervals first
keep if strpos(v1, "Conventional")|strpos(v1, "Eff. Number of obs |")

replace v1=subinstr(v1, "Eff. Number of obs |","",.)
replace v1=subinstr(v1, "Conventional |","",.)
replace v1=subinstr(v1, "VCE method    =  NNcluster","",.)




split v1, p(" ") gen(data)

keep data*

foreach v of varlist _all{
replace `v'="|"+`v' if `v'!=""
}

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32+data33

capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31+data32
			
capture gen results=data1+data2+data3+data4+data5+data6+data7+data8+data9+data10+data11+data12+data13+data14+data15+data16 ///
			+data17+data18+data19+data20+data21+data22+data23+data24+data25+data26+data27+data28+data29+data30+data31
			
keep results*

split results, p("|")

gen stat="coeff" if _n==2|_n==4
replace stat="N" if _n==1|_n==3

gen low=results6[2] if _n<3
gen up=results7[2]  if _n<3

replace low=results6[4] if _n>2&_n<5
replace up=results7[4] if _n>2&_n<5


gen NL=results2[1] if _n<3
gen NR=results3[1] if _n<3
replace NL=results2[3] if _n>2&_n<5
replace NR=results3[3] if _n>2&_n<5


gen coef2=results2[2] if _n<3
replace coef2=results2[4] if _n>2&_n<5

keep if stat=="coeff"


gen low_regular95=real(low)
gen up_regular95=real(up) 
gen eff_N_left=real(NL)
gen eff_N_right=real(NR)
gen coeff=real(coef2)


keep low_-coeff


gen sample="all years" if _n==1
replace sample="<2012" if _n==2

gen DV="`j'"


save "ppl_results`j'.dta", replace
}


****merge the results
use "ppl_resultseu2.dta", clear
save "FINAL_ppl_results.dta", replace

		
local DV eu3

foreach j of local DV {
use "FINAL_ppl_results.dta", clear
merge m:m DV sample using "ppl_results`j'.dta"
drop _merge
save "FINAL_ppl_results.dta", replace

}

	**Graph the results
use "FINAL_ppl_results.dta", clear	

	
*declare dataset as matrix

tostring eff_N_left, gen(s_eff_N_left)
tostring eff_N_right, gen(s_eff_N_right)

gen effective_number="N(L)="+s_eff_N_left+"      "+"N(R)="+s_eff_N_right

replace sample="before_2012" if sample=="<2012"

		sort DV sample
		
mkmat coef low_regular95 up_regular95 if sample=="all years"&DV=="eu2", mat(all_years)
  mkmat coef low_regular95 up_regular95 if sample=="before_2012"&DV=="eu2", mat(before_2011)

 coefplot (matrix(all_years[,1]), ci((all_years[,2] all_years[,3]))) ///
		(matrix(before_2012[,1]), ci((before_2012[,2] before_2012[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
		xscale(range(-.12 .12)) ///
						yscale(off) ///
		legend (order(2 "All years" 4 "Before 2012")) ///
		fxsize(100) title("Important problem") saving(fig1_ppl_eu2, replace)
		
mkmat coef low_regular95 up_regular95 if sample=="all years"&DV=="eu3", mat(all_years)
  mkmat coef low_regular95 up_regular95 if sample=="before_2012"&DV=="eu3", mat(before_2011)

 coefplot (matrix(all_years[,1]), ci((all_years[,2] all_years[,3]))) ///
		(matrix(before_2012[,1]), ci((before_2012[,2] before_2012[,3]))), ///
		xline(0, lcolor(red) lwidth(thin)) ///
		mlabel(effective_number) mlabposition(15) mlabsize(tiny) mlabgap(0) msize(small) ///
		graphregion(color(white)) ylab(,nogrid) ysize(1.5) xsize(2) ///
						yscale(off) ///
		legend (order(2 "All years" 4 "Before 2012")) ///
		fxsize(100) title("Important problem-ordinal") saving(fig1_ppl_eu3, replace)
		
		 grc1leg fig1_ppl_eu2.gph fig1_ppl_eu3.gph, cols(3) graphregion(color(white)) saving(fig1_ppl_FINAL, replace)

 graph export "FigureA4.pdf", replace
 
 
